﻿/*
 * Creado por SharpDevelop.
 * Usuario: Seba
 * Fecha: 30/10/2012
 * Hora: 09:03 a.m.
 * 
 * Para cambiar esta plantilla use Herramientas | Opciones | Codificación | Editar Encabezados Estándar
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Prueba_Sumas
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			if(txt_materiales.Text == "") { txt_materiales.Text = "0"; };
			if(txt_flete.Text == "") { txt_flete.Text = "0"; };
			if(txt_trabajos.Text == "") { txt_trabajos.Text = "0"; };
			
			try {
				double total = Convert.ToDouble(txt_materiales.Text) + Convert.ToDouble(txt_flete.Text) + Convert.ToDouble(txt_trabajos.Text);
				txt_total.Text = Convert.ToString(total);
			} catch {}
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			double HS_IngC = Convert.ToDouble(txt_HS1.Text);
			double HS_IngR = Convert.ToDouble(txt_HS2.Text);
			
			double Porcentaje = (HS_IngR * 100) / HS_IngC;
			
			lbl_Porc.Text = "% "+ Math.Round(Porcentaje,2).ToString();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			textBox2.Text = textBox1.Text.Substring(2, textBox1.Text.Length-2);
		}
		
		void TextBox3KeyPress(object sender, KeyPressEventArgs e)
		{
			//#region SOLO NUMEROS
			//if (Char.IsDigit(e.KeyChar)) {
			//	e.Handled = false;
			//} else if (Char.IsControl(e.KeyChar)) { 
			//	e.Handled = false;   
			//} else { 
			//	e.Handled = true; 
			//}
			//#endregion	
			
			#region METODO 2
			//if (char.IsNumber(e.KeyChar) || e.KeyChar=='.')
 			//{
  			//	if (System.Text.RegularExpressions.Regex.IsMatch(textBox3.Text, "^\\d*\\.\\d{2}$")) 
  			//		e.Handled=true;
			//	} else {
			//		e.Handled = e.KeyChar!=(char)Keys.Back;
			//	}
			//}
			#endregion
			
			#region METODO 2a

			if (Char.IsDigit(e.KeyChar)) {
				e.Handled = false;
				
				if(textBox3.Text.Length >=3) {
					if (textBox3.Text.Substring(textBox3.Text.Length-3,1) ==".") {
						//MessageBox.Show(textBox3.Text.Substring(textBox3.Text.Length-3,1));
						e.Handled=true;
						return;
					} 
				}				
				
			} else if (Char.IsControl(e.KeyChar)) { 
				e.Handled = false;   
			} else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
 			{


 				TextBox tb = sender as TextBox;
 				int cursorPosLeft = tb.SelectionStart;
 				int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
 				string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
 				string[] parts = result.Split('.');
 
				if (parts.Length > 1)
 				{
 					if (parts[1].Length > 2 || parts.Length > 2)
 					{
 						e.Handled = true;
 					}
 				}
			}  else { 
				e.Handled = true; 
			}
			#endregion			
			
			#region METODO 3
			
			//if (Char.IsControl(e.KeyChar))
			//{
 				//
 			//} else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
 			//{
 			//	TextBox tb = sender as TextBox;
 			//	int cursorPosLeft = tb.SelectionStart;
 			//	int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
 			//	string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
 			//	string[] parts = result.Split('.');
 
			//	if (parts.Length > 1)
 			//	{
 			//		if (parts[1].Length > 2 || parts.Length > 2)
 			//		{
 			//			e.Handled = true;
 			//		}
 			//	}
			//}
			#endregion
		}
			
		void TextBox4KeyPress(object sender, KeyPressEventArgs e)
		{
        	#region SOLO LETRAS
			if (Char.IsLetter(e.KeyChar)) {
               	e.Handled = false;
           	} else if (Char.IsControl(e.KeyChar)) {
               	e.Handled = false;
           	} else if (Char.IsSeparator(e.KeyChar)) {
            	e.Handled = false;
           	} else {
            	e.Handled = true;
           	}
			#endregion			
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			MessageBox.Show(textBox5.Text.Substring(textBox5.Text.Length-3,1));
		}
	}
}
